﻿using System;
using System.Windows;
using Phone7.Fx.Controls;

namespace Wp7nl.Utilities
{
    /// <summary>
    /// Helper class with attached dependency properties to let an icon bar icon
    /// change image based upon a value
    /// </summary>
    public static class AppBarIconFlipper
    {
        #region IconUri

        public static readonly DependencyProperty IconUriProperty =
          DependencyProperty.RegisterAttached("IconUri",
          typeof(Uri),
          typeof(AppBarIconFlipper),
          new PropertyMetadata(IconUriPropertyChanged));

        // Called when Property is retrieved
        public static Uri GetIconUri(DependencyObject obj)
        {
            return obj.GetValue(IconUriProperty) as Uri;
        }

        // Called when Property is set
        public static void SetIconUri(
           DependencyObject obj,
           Uri value)
        {
            obj.SetValue(IconUriProperty, value);
        }

        // Called when property is changed
        private static void IconUriPropertyChanged(
         object sender,
         DependencyPropertyChangedEventArgs args)
        {
            var attachedObject = sender as BindableApplicationBarIconButton;
            if (attachedObject == null) return;
            attachedObject.IconUri = (bool)attachedObject.GetValue(ShowAlternateIconUriProperty)
                                         ? (Uri)attachedObject.GetValue(AlternateIconUriProperty)
                                         : (Uri)args.NewValue;
        }

        #endregion

        #region AlternateIconUri
        public static readonly DependencyProperty AlternateIconUriProperty =
          DependencyProperty.RegisterAttached("AlternateIconUri",
          typeof(Uri),
          typeof(AppBarIconFlipper),
          new PropertyMetadata(AlternateIconUriPropertyChanged));

        // Called when Property is retrieved
        public static Uri GetAlternateIconUri(DependencyObject obj)
        {
            return obj.GetValue(AlternateIconUriProperty) as Uri;
        }

        // Called when Property is set
        public static void SetAlternateIconUri(
           DependencyObject obj,
           Uri value)
        {
            obj.SetValue(AlternateIconUriProperty, value);
        }

        // Called when property is changed
        private static void AlternateIconUriPropertyChanged(
         object sender,
         DependencyPropertyChangedEventArgs args)
        {
            var attachedObject = sender as BindableApplicationBarIconButton;
            if (attachedObject == null) return;
            attachedObject.IconUri = (bool)attachedObject.GetValue(ShowAlternateIconUriProperty)
                                         ? (Uri)args.NewValue
                                         : (Uri)attachedObject.GetValue(IconUriProperty);
        }
        #endregion

        #region ShowAlternateIconUri

        public static readonly DependencyProperty ShowAlternateIconUriProperty =
          DependencyProperty.RegisterAttached("ShowAlternateIconUri",
          typeof(bool),
          typeof(AppBarIconFlipper),
          new PropertyMetadata(ShowAlternateIconUriPropertyChanged));

        // Called when Property is retrieved
        public static bool GetShowAlternateIconUri(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowAlternateIconUriProperty);
        }

        // Called when Property is set
        public static void SetShowAlternateIconUri(
           DependencyObject obj,
           bool value)
        {
            obj.SetValue(ShowAlternateIconUriProperty, value);
        }

        // Called when property is changed
        private static void ShowAlternateIconUriPropertyChanged(
         object sender,
         DependencyPropertyChangedEventArgs args)
        {
            var attachedObject = sender as BindableApplicationBarIconButton;
            if (attachedObject == null) return;
            var value = (bool)args.NewValue;
            attachedObject.IconUri = value
                                         ? (Uri)attachedObject.GetValue(AlternateIconUriProperty)
                                         : (Uri)attachedObject.GetValue(IconUriProperty);
        }
        #endregion

    }
}
